package com.example.demo.test;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.HttpMethod;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.Protocol;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import java.net.URL;
import java.time.Instant;
import java.util.Date;
public class GeneratePresignedUrl {
    public static String ACCESS_KEY = "597TPSH9P92HGHDVAMMY";
    public static String SECRET_KEY = "UvsQnXTbC7jKkfcBzlQ1q9Ccry5LUfXJFLM2QszR";
    public static String END_POINT = "https://huabei-2.zos.ctyun.cn";
    public static String Bucket = "bucket-meizi";
    public static String Key = "1735982106105323/1735982106105323/picture/b0fdc34c-f360-4802-9d03-75a111eb876b.jpg";
//    public static String Key = "upload/vod/user1735982106105323/1736325987913009/origin.mp4?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20250109T011500Z&X-Amz-SignedHeaders=host&X-Amz-Expires=14400&X-Amz-Credential=597TPSH9P92HGHDVAMMY%2F20250109%2F%2Fs3%2Faws4_request&X-Amz-Signature=bd580873c8310d35ed10f4e2713cb488b0506f616cd086963501a6712bddf286";
    public static void main(String[] args) {
        AmazonS3 s3Client = null;
        try {
            AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY,
                    SECRET_KEY);
            ClientConfiguration awsClientConfig = new ClientConfiguration();
            awsClientConfig.setSignerOverride("AWSS3V4SignerType");
            awsClientConfig.setProtocol(Protocol.HTTP);
            s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new
                            AWSStaticCredentialsProvider(credentials))
                    .withClientConfiguration(awsClientConfig)
                    .withEndpointConfiguration(new
                            AwsClientBuilder.EndpointConfiguration(END_POINT, ""))
                    .disableChunkedEncoding()
                    .enablePathStyleAccess()
                    .build();
            System.out.print("=====connect success=====\n");
//获取预签名Url
            GeneratePresignedUrlRequest var = new
                    GeneratePresignedUrlRequest(Bucket, Key);
            Date expiration = new Date();
            long expTimeMillis = Instant.now().toEpochMilli();
            expTimeMillis += 1000 * 60 * 60;
// 设置有效期60分钟
            expiration.setTime(expTimeMillis);
            var.setExpiration(expiration);
            var.setMethod(HttpMethod.GET);
            URL url = s3Client.generatePresignedUrl(var);
//            创建上传链接并上传对象
            System.out.print(url.toString() + "\n");
            System.out.print(url.getProtocol() + "\n");
            System.out.print(url.getHost() + "\n");
            System.out.print(url.getPort() + "\n");
            System.out.print(url.getPath() + "\n");
            System.out.print("=====request success=====\n");
        }catch (Exception e) {
            System.out.print("=====request fail=====\n");
            System.out.print(e.getMessage());
        }
    }

    public static String getS3Url(String url) {
        AmazonS3 s3Client = null;
        try {
            AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY,
                    SECRET_KEY);
            ClientConfiguration awsClientConfig = new ClientConfiguration();
            awsClientConfig.setSignerOverride("AWSS3V4SignerType");
            awsClientConfig.setProtocol(Protocol.HTTP);
            s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new
                            AWSStaticCredentialsProvider(credentials))
                    .withClientConfiguration(awsClientConfig)
                    .withEndpointConfiguration(new
                            AwsClientBuilder.EndpointConfiguration(END_POINT, ""))
                    .disableChunkedEncoding()
                    .enablePathStyleAccess()
                    .build();
            System.out.print("=====connect success=====\n");
//获取预签名Url
            GeneratePresignedUrlRequest var = new
                    GeneratePresignedUrlRequest(Bucket, url);
            Date expiration = new Date();
            long expTimeMillis = Instant.now().toEpochMilli();
            expTimeMillis += 1000 * 60 * 60;
// 设置有效期60分钟
            expiration.setTime(expTimeMillis);
            var.setExpiration(expiration);
            var.setMethod(HttpMethod.GET);
            URL url2 = s3Client.generatePresignedUrl(var);
            url = url2.toString();
//            创建上传链接并上传对象
            System.out.print(url2.toString() + "\n");
//            System.out.print(url.getProtocol() + "\n");
//            System.out.print(url.getHost() + "\n");
//            System.out.print(url.getPort() + "\n");
//            System.out.print(url.getPath() + "\n");
            System.out.print("=====request success=====\n");
        }catch (Exception e) {
            System.out.print("=====request fail=====\n");
            System.out.print(e.getMessage());
        }
        return url;
    }
}